class Solution {
public:
	bool NextPermutation(vector<int>& num) {
        int n = num.size();
        int pos = n - 1;
        while (pos > 0 && num[pos - 1] >= num[pos]) {
            --pos;
        }
        if (pos == 0) {
            return false;
        }
        int pos2 = n - 1;
        while (num[pos2] <= num[pos - 1]) {
            --pos2;
        }
        swap(num[pos - 1], num[pos2]);
        reverse(num.begin() + pos, num.end());
        return true;
    }

    vector<vector<int> > permuteUnique(vector<int> &num) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
		vector<vector<int> > result;
        sort(num.begin(), num.end());

        result.push_back(num);
        while (NextPermutation(num)) {
            result.push_back(num);
        }
        return result;

    }
};